<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>资源管理</el-breadcrumb-item>
        <el-breadcrumb-item>索引管理</el-breadcrumb-item>
      </el-breadcrumb>

      <el-row :gutter="20">
        <el-col :span="2">
          <el-button type="primary" @click="addClick">添加索引</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="danger" @click="batchDeleteJxIndex">批量删除</el-button>
        </el-col>
      </el-row>

      <!-- 索引表格区 -->
      <el-table
        :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }"
        :data="tableData"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="id"
          label="序号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="inforomationsort"
          label="资源分类"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="索引名称">
        </el-table-column>
        <el-table-column
          prop="orderId"
          label="orderId">
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
              type="primary" 
              icon="el-icon-edit" 
              size="mini"
              @click="editClick(scope.row)"></el-button>
            <el-button 
              type="danger" 
              icon="el-icon-delete" 
              size="mini" 
              @click="delClick(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>

    <!-- 操作索引对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="opeDialogVisible"
      width="50%">
      <el-form
        :model="opeForm"
        ref="opeFormRef"
        label-width="100px">
        <el-form-item label="资源分类" prop="inforomationsort">
          <el-input v-model="opeForm.inforomationsort"></el-input>
        </el-form-item>
        <el-form-item label="索引名称" prop="name">
          <el-input v-model="opeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="orderId" prop="orderId">
          <el-input v-model="opeForm.orderId"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="opeForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="opeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="operation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getAllIndex()
  },
  data() {
    return {
      tableData: [],
      total: 0,
      title: '',
      opeDialogVisible: false,
      opeForm: {
        inforomationsort: 0,
        name: "",
        orderId: 0,
        remark: ""
      },
      batchList: []
    }
  },
  methods: {
    async getAllIndex() {
      const { data: res } = await this.$http.post('allJxIndex')
      if (res.code != 'success') return this.$message.error('获取索引失败')
      this.tableData = res.data
      this.total = res.numbers
      this.$message.success('获取索引成功')
    },
    addClick() {
      this.title = '添加索引'
      this.opeDialogVisible = true
    },
    editClick(index) {
      this.title = '修改索引'
      this.opeForm.id = index.id
      this.opeForm.inforomationsort = index.inforomationsort
      this.opeForm.name = index.name
      this.opeForm.orderId = index.orderId
      this.opeForm.remark = index.remark
      this.opeDialogVisible = true
    },
    // 索引的增加和修改
    async operation() {
      if (this.title == '添加索引') {
        const { data: res } = await this.$http.post('insertJxIndex', this.opeForm)
        if (res != 'success') return this.$message.error('添加索引失败')
        this.opeDialogVisible = false
        this.$message.success('添加索引成功')
        this.getAllIndex()
      } else {
        const { data: res } = await this.$http.post('updateJxindex', this.opeForm)
        if (res != 'success') return this.$message.error('修改索引失败')
        this.opeDialogVisible = false
        this.$message.success('修改索引成功')
        this.getAllIndex()
      }
    },
    // 单个删除
    async delClick(id) {
      const confirmResult = await this.$confirm('此操作将永久删除索引, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteJxIndex', { ids: id })
      if (res != 'success') return this.$message.error('删除索引失败')
      this.$message.success('删除索引成功')
      this.getAllIndex()
    },
    // 批量删除
    async batchDeleteJxIndex() {
      var ids = []
      this.batchList.forEach(item => ids.push(item.id))
      const confirmResult = await this.$confirm('此操作将永久删除索引, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteJxIndex', { ids: ids.join(',') })
      if (res != 'success') return this.$message.error('删除索引失败')
      this.$message.success('删除索引成功')
      this.getAllIndex()
    },
    handleSizeChange() {},
    handleCurrentChange() {},
    handleSelectionChange(indexs) {
      this.batchList = indexs
    }
  }
} 
</script>